package com.appspot.chooserepository.server;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailHandler {
	
	public final static String SENDER_ADDRESS = "chooserepository.app@gmail.com";
	public final static String SENDER_NAME = "Choose repository application";
	
	public static void sendMail(RepositoryUserData repositoryUserData) {
	    Properties props = new Properties();
	    Session session = Session.getDefaultInstance(props, null); 
	    try {             
	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(SENDER_ADDRESS, SENDER_NAME)); 
	    	InternetAddress toAddress = new InternetAddress("horomena@bezeqint.net");
	    	msg.addRecipient(Message.RecipientType.TO, toAddress);             
	    	msg.setSubject(createMessageSubject(repositoryUserData));            
	    	msg.setText(createMessageBody(repositoryUserData));             
	    	Transport.send(msg);          
	    } catch (AddressException e) {  
	   
	    } catch (MessagingException e) {
	    	
	    } catch (UnsupportedEncodingException e) {
	    	
	    }
	}
	
/*	private static String createMessagePersonal(RepositoryUserData repositoryUserData) {
		return  "Mr. User";
	}*/
	
	private static String createMessageSubject(RepositoryUserData repositoryUserData) {
		return "Choose repository reminder";
	}
	
	private static String createMessageBody(RepositoryUserData repositoryUserData) {
		StringBuffer msgBody = new StringBuffer();
		msgBody.append("Hi " + repositoryUserData.getUser() + ", \n");
		msgBody.append("You are logged to chooserepository.appspot.com since " + repositoryUserData.getLastUpdated() + "\n");
		msgBody.append("If you finished using the repository please remove your entry from the website \n");
		msgBody.append("Thanks, \n");
		msgBody.append("chooserepository.appspot.com");
		return msgBody.toString();
	}
}

